using System;
using System.Web.UI.WebControls;
using System.Web.UI;
using System.ComponentModel;
using System.ComponentModel.Design;
using System.Globalization;
using AjaxControlToolkit;

[assembly: System.Web.UI.WebResource("AjaxControlToolkitEx.TopMostPanelExtenderBehavior.js", "text/javascript")]

namespace AjaxControlToolkitEx
{
    [RequiredScript(typeof(CommonToolkitScripts))]
    [Designer(typeof(TopMostPanelExtenderDesigner))]
    //[ClientScriptResource("AjaxControlToolkitEx.TopMostPanelExtenderBehavior", "AjaxControlToolkitEx.TopMostPanelExtenderBehavior.js")]
    [ClientScriptResource("AjaxControlToolkitEx.TopMostPanelExtenderBehavior", "AjaxControlToolkitEx.TopMostPanelExtenderBehavior")]
    [TargetControlType(typeof(Control))]
    public class TopMostPanelExtender : ExtenderControlBaseEx
    {
        // TODO: Add your property accessors here.
        //

        [DefaultValue(false)]
        [ExtenderControlProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public bool DisableOnPostBack
        {
            get { return GetPropertyValue("DisableOnPostBack", false); }
            set { SetPropertyValue("DisableOnPostBack", value); }
        }

        [DefaultValue("")]
        [ExtenderControlProperty]
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1706:ShortAcronymsShouldBeUppercase", Justification = "Following ASP.NET AJAX pattern")]
        public string HiddenFieldID
        {
            get { return GetPropertyValue("HiddenFieldID", ""); }
            set { SetPropertyValue("HiddenFieldID", value); }
        }

        protected string HFID
        {
            get { return ID + "hf"; }
        }

        protected override void OnInit(EventArgs e)
        {
            CreateClientHiddenField();
            Page.PreLoad += new EventHandler(Page_PreLoad);
            base.OnInit(e);
        }

        void Page_PreLoad(object sender, EventArgs e)
        {
            if (((Page)sender).IsPostBack)
            {
                LoadClientHiddenFieldValue();
            }
        }
        private void CreateClientHiddenField()
        {
            ExtenderControlBaseEx.HiddenFieldEx field = new ExtenderControlBaseEx.HiddenFieldEx();
            field.ID = HFID;
            Controls.Add(field);
            HiddenFieldID = GetClientID(HFID);
        }
        private string Value = "1";
        private void LoadClientHiddenFieldValue()
        {
            ExtenderControlBaseEx.HiddenFieldEx hiddenField = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFID);

            if ((hiddenField != null) && !string.IsNullOrEmpty(hiddenField.Value))
            {
                Value = hiddenField.Value;
            }
        }
        protected void SaveClientClientHiddenFieldValue()
        {
            ExtenderControlBaseEx.HiddenFieldEx hiddenField = (ExtenderControlBaseEx.HiddenFieldEx)NamingContainer.FindControl(HFID);

            if (hiddenField != null)
            {
                hiddenField.Value = Value;
            }
        }
        protected override void CreateChildControls()
        {
            base.CreateChildControls();
            this.BehaviorID = GetClientID(ID) + "_behaviour";
        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            SaveClientClientHiddenFieldValue();
        }

    }
}
